package schema

import (
	"entgo.io/ent"
	"entgo.io/ent/schema/edge"
	"entgo.io/ent/schema/field"
	"entgo.io/ent/schema/index"
)

type Region struct {
	ent.Schema
}

type OsmData struct {
	MapLink          string                `json:"mapLink"`
	BoundingBox      []float64             `json:"boundingBox,omitempty"`
	PointsOfInterest []OsmPointsOfInterest `json:"pointsOfInterest,omitempty"`
}

type OsmPointsOfInterest struct {
	Name      string  `json:"name"`
	Type      string  `json:"type"`
	Latitude  float64 `json:"latitude,string"`
	Longitude float64 `json:"longitude,string"`
}

func (Region) Fields() []ent.Field {
	return []ent.Field{
		field.Int64("id").StorageKey("id"),
		field.String("name"),
		field.String("parentRegion").Optional(),
		field.Float("latitude1").Optional(),
		field.Float("longitude1").Optional(),
		field.Float("latitude2").Optional(),
		field.Float("longitude2").Optional(),
		field.JSON("osmData", OsmData{}).Optional(),
	}
}

func (Region) Edges() []ent.Edge {
	return []ent.Edge{
		edge.To("regionType", RegionType.Type).Unique(),
		edge.To("accountId", User.Type).Unique(),
	}
}

func (Region) Indexes() []ent.Index {
	return []ent.Index{
		index.Fields("name"),
		index.Fields("parentRegion"),
		index.Edges("regionType"),
	}
}
